<script setup lang="ts">
import { onMounted, ref } from 'vue'

import { categorys, articles } from '@/models/article'

//用户搜索时选中的分类id
const categoryId = ref('')
//用户搜索时选中的发布状态
const state = ref('')

//分页条数据模型
const pageNum = ref(1)//当前页  
const total = ref(20)//总条数
const pageSize = ref(10)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size: number) => {
  pageSize.value = size
  console.log(pageSize.value);
  fetchArticleList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num: number) => {
  pageNum.value = num
  fetchArticleList()
}
//获取文章分类列表
import useCategory from '@/hooks/useCategory';
const { getCategory } = useCategory()

//获取文章列表
import { articleListService } from "@/api/article";
import { ElMessage } from 'element-plus';
const fetchArticleList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    categoryId: categoryId.value ? categoryId.value : null,
    state: state.value ? state.value : null
  }
  try {
    let result = await articleListService(params)
    total.value = result.data.data.total
    articles.value = result.data.data.items
    // 获取文章分类名称
    for (let i = 0; i < articles.value.length; i++) {
      let article = articles.value[i]
      // console.log(article);
      for (let j = 0; j < categorys.value.length; j++) {
        if (article.categoryId == categorys.value[j].id) {
          article.categoryName = categorys.value[j].categoryName
        }
      }
    }
  } catch (error) {
    console.log(error);
  }
}
onMounted(async () => {
  getCategory()
  fetchArticleList()
})

// // 控制抽屉是否显示
// const visibleDrawer = ref(false)
// // 更新抽屉状态
// const updateVisibleDrawer = (newValue: any) => {
//   visibleDrawer.value = newValue
// }
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>文章管理</span>
        <!-- <div class="extra">
          <el-button type="primary" @click="updateVisibleDrawer">添加文章</el-button>
          <ArticleDrawer v-model="visibleDrawer" @update-visibleDrawer="updateVisibleDrawer" :fetchArticleList />
        </div> -->
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <el-select placeholder="请选择" v-model="categoryId" style="width: 120px">
          <el-option v-for="i in categorys" :key="i.id" :label="i.categoryName" :value="i.id">
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="发布状态：">
        <el-select placeholder="请选择" v-model="state" style="width: 120px">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="fetchArticleList()">搜索</el-button>
        <el-button @click="categoryId = '', state = '', fetchArticleList()">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="articles" style="width: 100%">
      <el-table-column label="文章标题" width="400" prop="title"></el-table-column>
      <el-table-column label="分类" prop="categoryName"></el-table-column>
      <el-table-column label="作者" prop="username"></el-table-column>
      <el-table-column label="发表时间" prop="createTime"> </el-table-column>
      <!-- <el-table-column label="状态" prop="state"></el-table-column> -->
      <!-- <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary"></el-button>
          <el-button :icon="Delete" circle plain type="danger"></el-button>
        </template>
      </el-table-column> -->
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :total="total" :page-sizes="[5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next" background @size-change="onSizeChange"
      @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
  </el-card>
</template>

<style scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>